package top.milkbox.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.milkbox.info.R;
import top.milkbox.info.requestBody.CodeAndEmail;
import top.milkbox.service.EmailVerificationCodeService;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/emailVC")
public class EmailVerificationCodeController {

    @Autowired
    private EmailVerificationCodeService emailVCS;

    /**
     * 发送邮件
     *
     * @param session      session
     * @param codeAndEmail 邮箱验证码和邮箱
     * @return R
     */
    @PostMapping("/send")
    public R sendEmail(
            HttpSession session,
            @RequestBody CodeAndEmail codeAndEmail
    ) {
        R r = new R();
        Boolean isSend = emailVCS.sendEmail(session, codeAndEmail.getCode(), codeAndEmail.getEmail());
        r.setFlag(isSend);
        if (isSend) {
            r.setMessage("邮件发送成功，请注意查收");
        } else {
            r.setMessage("图片验证码错误，请重试");
        }
        return r;
    }
}
